package com.barber.service;


import com.barber.entity.Barber;
import com.barber.entity.Order;
import com.barber.mapper.BarberMapper;
import com.barber.mapper.OrderMapper;
import com.barber.util.XtUtil;
import com.fasterxml.jackson.datatype.jsr310.deser.key.OffsetDateTimeKeyDeserializer;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.awt.image.OffScreenImage;

import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    Logger logger=Logger.getLogger(OrderServiceImpl.class);
    @Autowired
    OrderMapper orderMapper;

    @Override
    public boolean insertOrder(Order order) {
        logger.debug(order);



        order.setBegintime(XtUtil.toDatetimeString(order.getBegintime()));
        order.setEndtime(XtUtil.toDatetimeString(order.getEndtime()));
        logger.debug(order);//Order{id=1112, barberid=900, customerid=12, status=0, begintime='2024-03-29 16:00:00.0', endtime='2024-03-29 16:00:00.0', styleid=0}
        boolean b = orderMapper.insert(order);
        logger.debug(b);
        return b;
    }

    @Override
    public boolean checktime(Order order) {
        logger.debug(order);
        order.setBegintime(XtUtil.toDatetimeString(order.getBegintime()));
        order.setEndtime(XtUtil.toDatetimeString(order.getEndtime()));
     Integer b = orderMapper.checkTime(order);
     if(b!=null)
         return true;
         else
        return false;
    }

    @Override
    public boolean deleteOrder(Map map) {
        logger.debug(map);
        boolean b = orderMapper.delete(map);
        logger.debug(b);
        return b;
    }

    @Override
    public Map getOrders(Map map) {
        logger.debug(map);

        int id =0;//id是理发师，和模糊查询时候用
//        String key = (int) map.get("key");//模糊查询时候用未完善
        int usertype = (int) map.get("usertype");
        int userid = 0;
//
//        String keyS=(String) map.get("key");
//
//        Integer key=0;
        if(usertype==2) {
             userid = (int) map.get("userid");
            logger.debug(userid);
//            key=Integer.parseInt(keyS);模糊没成功
        }
//        else if(usertype==1){
//
//        }
        else if(usertype==3){//理发师
            id = (int) map.get("id");//id是理发师
//            key=Integer.parseInt(keyS);模糊没成功

        }


        int pageIndex = 0;
        int pageSize = 10;

        try {
            pageIndex = Integer.parseInt((String) map.get("pageIndex"));
        } catch (Exception e) {
            // 处理异常或设置默认值
        }

        try {
            pageSize = Integer.parseInt((String) map.get("pagesize"));
        } catch (Exception e) {
            // 处理异常或设置默认值
        }

        // 计算分页的偏移量
        int offset = pageIndex * pageSize;

        // 查询数据列表
        List<Order> orderList = orderMapper.selectOrdersByName(id,usertype,userid,offset, pageSize);
        logger.debug(orderList);
        // 查询总数
        int totalCount = orderMapper.selectOrderCountByName(id);
        logger.debug(totalCount);
        // 将结果放入HashMap中
        map.put("total", totalCount);//pageIndex=0, sortField=id, sortOrder=asc, pageSize=10, total=23,
        map.put("data", orderList);

        return map;
    }

    @Override
    public boolean updateOrder(Order order) {
        logger.debug(order);
        int type= (int) order.getUsertype("usertype");
        boolean b=false;
        if(type==1) {//管理员
             b = orderMapper.update(order);
            logger.debug(b);
        }
        if(type==3) {//理发师
            b = orderMapper.update(order);
            logger.debug(order);
        }
        return b;
    }

    @Override
    public boolean cancelOrder(Map map) {
        logger.debug(map);
        boolean b = orderMapper.cancel(map);
        logger.debug(b);
        return b;
    }
//    @Autowired
//    BarberMapper barberMapper;
//    @Override
//    public Barber selectAllBarber(Barber barber) {
//        logger.debug(barber);
////        barber= (Barber) barberMapper.selectAllBarber();
////        logger.debug(barber);
//        return barber;
//    }


}
